/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigs.web.admin.version;

import java.util.List;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.chigs.domain.Product;
import com.googlecode.chigs.domain.Variant;
import com.googlecode.chigs.domain.Version;
import com.googlecode.chigs.repository.GenericDomainService;
import com.googlecode.chigs.web.common.IconFactory;
import com.googlecode.chigs.web.common.forms.ShinyForm;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
@AuthorizeInstantiation("ROLE_ADMIN")
public class EditOrCreateVersionPanel extends Panel {
	@SpringBean
	private GenericDomainService genericDomainService;

	public EditOrCreateVersionPanel(String id) {
		this(id, new CompoundPropertyModel<Version>(new Version()));
	}

	public EditOrCreateVersionPanel(String id, IModel<Version> model) {
		super(id);
		add(new EditOrCreateVersionForm("form", model));
	}

	class EditOrCreateVersionForm extends ShinyForm<Version> {
		public EditOrCreateVersionForm(String id, IModel<Version> model) {
			super(id, new CompoundPropertyModel<Version>(model.getObject()));
			StringResourceModel lMajor = new StringResourceModel("major.label", EditOrCreateVersionForm.this, null);
			add(new Label("major.label", lMajor));
			add(new TextField<Integer>("major")
					.setRequired(true)
					.setLabel(lMajor));

			StringResourceModel lMinor = new StringResourceModel("minor.label", EditOrCreateVersionForm.this, null);
			add(new Label("minor.label", lMinor));
			add(new TextField<Integer>("minor")
					.setRequired(true)
					.setLabel(lMinor));
			
			List<Variant> variants = genericDomainService.loadAll(Variant.class);
			DropDownChoice<Variant> dd = new DropDownChoice<Variant>("variant", new PropertyModel<Variant>(getModelObject(), "variant"), variants, new ChoiceRenderer<Variant>("name", "id"));
			dd.setNullValid(true);
			add(dd);

			List<Product> products = genericDomainService.loadAll(Product.class);
			add(new DropDownChoice<Product>("product", new PropertyModel<Product>(getModelObject(), "product"), products, new ChoiceRenderer<Product>("name", "id"))
					.setRequired(true)
			);

			add(new Image("submit", IconFactory.createOkIconReference()));

			Link<Void> cancelLink = new Link<Void>("cancelButton") {
				@Override
				public void onClick() {
					returnToManagement();
				}
			};
			cancelLink.add(new Image("icon", IconFactory.createCancelIconReference()));
			add(cancelLink);
		}

		@Override
		protected void onSubmit() {
			try {
				genericDomainService.save(Version.class, getModelObject());
				getSession().info(new StringResourceModel("save.success", EditOrCreateVersionForm.this, null).getString());
				returnToManagement();
			} catch (Exception e) {
				error(e.getMessage());
			}
		}		

		private void returnToManagement() {
			final Panel p = new ManageVersionsPanel(EditOrCreateVersionPanel.this.getId());
			p.setOutputMarkupId(true);
			EditOrCreateVersionPanel.this.getParent().addOrReplace(p);
			this.setVisible(false);
		}
	}
}

